#!/usr/local/bin/python
# -*- coding=utf-8 -*-
import sys
import os
import logging
import logging.handlers

logLevels = {'debug':logging.DEBUG,'info':logging.INFO}

def getLogger(appName='main', logLevel='debug', fileName=None):
    projectPath = os.path.dirname(sys._getframe(1).f_globals.get('__file__'))
    if not fileName: fileName = '%s/logs/%s.log' %(projectPath,appName)

    logger = logging.getLogger(appName)

    #向文件输出
    logger.setLevel(logLevels.get(logLevel))
    formatter = logging.Formatter('%(asctime)s\tlogger:%(name)s\tlevel:%(levelname)s\tfun:%(module)s.%(funcName)s:%(lineno)d\tmessage:%(message)s', '')
    fileHandler= logging.handlers.TimedRotatingFileHandler(fileName,'midnight',1,7)
    fileHandler.setLevel(logLevels.get(logLevel))
    fileHandler.setFormatter(formatter)
    logger.addHandler(fileHandler)

    #向终端输出
    console = logging.StreamHandler()
    console.setLevel(logging.DEBUG)
    logger.addHandler(console)

    return logger
    
